<?php

register_shutdown_function(function() {
  $lastError = error_get_last();
  if (!empty($lastError)) {
    header("HTTP/1.1 500 Internal Server Error", true, 500);
  }
});

include '../config.inc.php';
include _FLEP_LIB_DIR_.'save.inc.php';

flep_session_start();

function save($data, $type) {

  if (!flep_structure($structure, $type)) {
    $_SESSION['error'] = "Error: Can't find type: $type";
  }

  $structure['data'][0] = &$data;
  flep_decode_list_id($structure);
  if(array_key_exists('clave', $data)){
    if($data['clave'] === '') {
      unset($data['clave']);
    } else {
      $data['clave'] = flep_pass($data['clave']);
    }
  }
  if (flep_save($structure)) {
    $_SESSION['msg'] = 'Guardado Exitosamente.';
  } else {
    $_SESSION['error'] = $structure['msg'];
  }

  header('Location: '.$_SESSION['on_save'][$type]);
}

if ((isset($_POST["id"]) || isset($_POST['data'][0]['id'])) && isset($_GET["type"])) {
    if (!flep_valid_login($_SESSION['user'])) {
        header("Location: index.php");
        exit();
    }
    save($_POST, $_GET["type"]);
    exit();
}

header("Location: index.php");
?>
